﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 11/04/2009
// Time: 21:14
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.common
{
	/// <summary>
	/// Description of simpleRegisterTemplate.
	/// </summary>
	public partial class simpleRegisterTemplate : KryptonForm
	{
		protected int _status;
		protected bool resizeForm = false;
		protected string _title;
		
		public simpleRegisterTemplate()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		#region Button Event`s
			private void BtnAddClick(object sender, EventArgs e)
			{
			   _status = CONSTANTS.stInsert;
				//Util.setButtons(buttons, _status);
				//Util.clearForm(this);
				txtName.Clear();
				txtName.Focus();
				
				//tabControl.SelectedTab = tabPrincipal;
				newEvent();				
			}
			
			private void BtnEditClick(object sender, EventArgs e)
			{
			   _status = CONSTANTS.stEdit;
			   
			   int index = dgvMain.SelectedRows.Count;
				if (index > 0)
				{
					txtName.Clear();
				   setEdits();
				   
				   txtName.Select();
				   txtName.Focus();
					//Util.setButtons(buttons, _status);		
				}
				else
					KryptonMessageBox.Show("Por favor selecione um registro!", _title);			
			}
			
			private void BtnSaveClick(object sender, EventArgs e)
			{
				bool result;
				
				if( Util.validateForm(this) && validate())
				{
					setClass();
					//Save
				   if( _status == CONSTANTS.stInsert )
				   {
				   	result = save();
				   }
				   else //Update
				   {
				   	result = update();
				   }
				   
			
					if( result )
					{
						//KryptonMessageBox.Show("Registro salvo com sucesso!",_title);
						fillDataGridView();
					   _status = CONSTANTS.stBrowse;   
						//Util.setButtons(buttons, _status);						
					}			
					else
					{
						if (Database.result.ErrorMessage.Contains("duplicate key violates unique constraint"))
							KryptonMessageBox.Show("Erro ao tentar salvar o registro:\nRegistro já existe!!!.",_title);						
						else
							KryptonMessageBox.Show("Erro ao tentar salvar o registro:\n"+Database.result.ErrorMessage,_title); 					
						return;
					}
				}		
			}
			
			private void BtnDeleteClick(object sender, EventArgs e)
			{
			   int index = dgvMain.SelectedRows.Count;
				if (index <= 0)
				{
					KryptonMessageBox.Show("Por favor selecione um registro!", _title);			
					return;
				}
				DialogResult dr = KryptonMessageBox.Show("Deseja realmente excluir o registro?",
				                                  _title,
				                                  MessageBoxButtons.YesNoCancel);
				                                  
				if( dr == DialogResult.Yes )			                                  
				{
					if( delete() )
					{
						//KryptonMessageBox.Show("Registro excluido com sucesso!", _title);
						fillDataGridView();
					}			
					else
					{
						if (Database.result.ErrorMessage.Contains("violates foreign key constraint"))
							KryptonMessageBox.Show("Erro ao tentar excluir o registro:\nRegistro já referenciado em outra tabela.",_title);
						else
							KryptonMessageBox.Show("Erro ao tentar excluir o registro:\n"+Database.result.ErrorMessage,_title);
					}
				}
			   _status = CONSTANTS.stBrowse;   
				//Util.setButtons(buttons, _status);					
			}
	
			private void BtnCancelClick(object sender, EventArgs e)
			{
				_status = CONSTANTS.stBrowse;
				
				//tabControl.SelectedTab = tabPesquisa;
				Util.clearForm(this);
				//Util.setButtons(buttons, _status);
			}
			
			private void BtnExitClick(object sender, EventArgs e)
			{
				this.Close();
				this.Dispose();
			}
		#endregion		
		
		#region virtual methods
			protected virtual void newEvent()
			{
				
			}
			/// <summary>
			/// Set data from edits to class
			/// </summary>
			protected virtual void setClass()
			{
			
			}
			
			/// <summary>
			/// Set data from class to edits
			/// </summary>
			protected virtual void setEdits()
			{
			
			}
			
			/// <summary>
			/// Fill the DataGridView
			/// </summary>
			protected virtual void fillDataGridView()
			{
			
			}
			
			/// <summary>
			/// Find the object of search
			/// </summary>
			protected virtual void find()
			{
			
			}		
			
		   /// <summary>
		   /// Save the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
		   protected virtual bool save()
		   {
		   	return false;
		   }
		   
		   /// <summary>
		   /// Update the register in the database
		   /// </summary>
		   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
		   protected virtual bool update()
		   {   
		   	return false;
		   }
		   
		   /// <summary>
		   /// Delete the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
		   protected virtual bool delete()
		   {
		   	return false;
		   }
		   
		   /// <summary>
		   /// Method call in initialization
		   /// </summary>
		   protected virtual void init()
		   {
		   
		   }
		   
		   protected virtual bool validate()
		   {
		   	   return true;
		   }
		#endregion
		
		private void SimpleRegisterTemplateLoad(object sender, EventArgs e)
		{
			init();
			if( !resizeForm )
			{
				this.MinimumSize = this.Size;
				this.MaximumSize = this.Size;
			}
			
			_status = CONSTANTS.stBrowse;
			
			//Util.setButtons(buttons, _status);
			fillDataGridView();
			
			_title = String.Empty; //System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;			
		}
	}
}
